package com.qidian.controller;

import com.qidian.pojo.QdClass;
import com.qidian.service.QdClassService;
import com.qidian.util.LayuiDataList;
import com.qidian.util.QidianResponse;
import com.qidian.vo.QdClassVO;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 戴着假发的程序员
 */
@RestController
@RequestMapping("/qdClass")
public class QdClassController {




    /**
     * 服务对象
     */
    @Resource
    private QdClassService qdClassService;


    @RequestMapping("/export-class")
    public void exportClass(Long classId, HttpServletRequest request,HttpServletResponse response){
        String serverPath = request.getServletContext().getRealPath("/");
        File tempFile = new File(serverPath+"/temp");
        if(!tempFile.exists() || !tempFile.isDirectory()){
            tempFile.mkdir();
        }
        QidianResponse r = qdClassService.exportClassInfo(tempFile.getAbsolutePath(), classId);
        if(r.getCode()==200){
            String fileName = r.getMsg();
            ServletContext servletContext = request.getServletContext();
            String mimeType = servletContext.getMimeType(fileName);
            response.setContentType(mimeType);
            response.setHeader("Content-Disposition","attachment;filename="+fileName);
            FileInputStream in = null;
            OutputStream out = null;
            try{
                in = new FileInputStream(serverPath+"/temp/"+fileName);
                out = response.getOutputStream();
                int len = -1;
                byte [] buff = new byte[1024];
                while((len = in.read(buff))!=-1){
                    out.write(buff,0,len);
                }
            }catch (IOException e){
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }finally {
                try{
                    if(in!=null)
                        in.close();;
                }catch (IOException ex){
                    ex.printStackTrace();
                }
            }
        }
    }

    /**
     * 通过主键查询单条数据
     *
     * @param classId
     * @return 单条数据
     */
    @RequestMapping(value = "get", method = RequestMethod.GET)
    public QidianResponse selectOne(Long classId) {
        return qdClassService.selectById(classId);
    }
    
    /**
     * 新增一条数据
     *
     * @param qdClass 实体类
     * @return Map
     */
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public QidianResponse insert(QdClass qdClass) {
        return qdClassService.insert(qdClass);
    }

    /**
     * 修改一条数据
     *
     * @param qdClass 实体类
     * @return Response对象
     */
    @RequestMapping(value = "update")
    public QidianResponse update(QdClass qdClass) {
        return qdClassService.update(qdClass);
    }

    /**
     * 删除一条数据
     *
     * @param qdClass 参数对象
     * @return Response对象
     */
    @RequestMapping(value = "delete")
    public Map delete(QdClass qdClass) {
        Map map = new HashMap();
        map.put("code",200);
        int result = qdClassService.deleteById(qdClass.getClassId());
        if (result <= 0) {
           map.put("code",500);
        }
        return map;
    }

     /**
     * 批量删除
     *
     * @param ids
     * @return Response对象
     */
    @RequestMapping(value = "deletes")
    public Map deletes(Long [] ids) {
        Map map = new HashMap();
        map.put("code",200);
        int result = qdClassService.deleteByIds(ids);
        if (result <= 0) {
           map.put("code",500);
        }
        return map;
    }

    /**
     * 查询全部
     *
     * @return Response对象
     */
    @RequestMapping(value = "selectAll")
    public List<QdClass> selectAll() {
        return qdClassService.selectAll();
    }

    /**
     * 分页查询
     *
     * @param page 偏移
     * @param limit 条数
     * @return List<QdClass>
     */
    @RequestMapping(value = "selectPage", method = RequestMethod.GET)
    public List<QdClass> selectPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10")Integer limit) {
        return qdClassService.selectPage(page, limit);
    }
    /**
     * 分页查询
     *
     * @param page 偏移
     * @param limit 条数
     * @return List<QdClass>
     */
    @RequestMapping(value = "selectPageByPojo", method = RequestMethod.GET)
    public List<QdClass> selectPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10")Integer limit,QdClass qdClass) {
        return qdClassService.selectPage(page, limit,qdClass);
    }

        /**
     * 分页查询
     *
     * @param page 偏移
     * @param limit 条数
     * @return
     */
    @RequestMapping(value = "selectPageForLayui")
    public LayuiDataList<QdClass> selectPageForLayui(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10")Integer limit, QdClassVO vo) {
        return qdClassService.selectPageForLayui(page, limit,vo);
    }

    
}